package com.jbm.jbmsupplier.zp.presenters;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.jbm.jbmsupplier.net.NetInterChat;
import com.jbm.jbmsupplier.shared.SharedPreferActivity;
import com.jbm.jbmsupplier.zp.entity.Buddy_ListData;
import com.jbm.jbmsupplier.zp.models.TestDuiInterfaces;
import com.jbm.jbmsupplier.zp.sqldata.MySqliteUtlis;
import com.jbm.jbmsupplier.zp.utils.ClassUser;
import com.jbm.jbmsupplier.zp.views.TestDuiViews;
import com.jbm.onehome.iface.ContactEntity;

import java.util.ArrayList;

import static com.jbm.jbmsupplier.zp.utils.TableName.BUDDY;

/**
 * Created by 家帮忙 on 2017/7/12.
 */

public class TestDuiActivitys implements TestDuiInterfaces{
    private Context context;
    private TestDuiViews testDuiViews;
    private SharedPreferActivity sharedPreferActivity;
   private ClassUser ClassUser;
    private MyBroadCat mybroad;

    private ArrayList<Buddy_ListData> Buddy_ListData = new ArrayList<>();
    private final int GAINFRIENT = 00001;
    private ClassUser classuser;

    public TestDuiActivitys(Context context, TestDuiViews testDuiViews) {
        this.context = context;
        this.testDuiViews = testDuiViews;
    }


    @Override
    public void InitData() {
        sharedPreferActivity=new SharedPreferActivity(context,true);
        ClassUser=new ClassUser(context);
//        if (sharedPreferActivity.getBoolearSql()==false) {
            /**
             * 首次联网获取好友
             */
            NetInterChat netInterChat = new NetInterChat(handler);
            netInterChat.getFriendList(GAINFRIENT, ClassUser.getLogin());
//        } else {
//            initLocalData();
//        }
       classuser= new ClassUser(context);
        mybroad = new MyBroadCat();
        IntentFilter intfile = new IntentFilter();
        intfile.addAction("AddContact");
        intfile.addAction("DropContact");
       context. registerReceiver(mybroad,intfile);

    }


    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case GAINFRIENT:
                    ContactEntity accctrl = (ContactEntity) msg.obj;
                    if (accctrl.m_code == 200) {
                        Buddy_ListData.clear();
                        if (accctrl.getContactList()==null){
                           testDuiViews.setUpdata();
                            return;
                        }else
                        if (accctrl.getContactList().size()<0){
                            return;
                        }else {
                            sharedPreferActivity.setBoolearSql(true);
                            MySqliteUtlis.GetInstan(context).deleteUserid(BUDDY,classuser.getLogin());
                            for (int i = 0; i < accctrl.getContactList().size(); i++) {
                            MySqliteUtlis.GetInstan(context).StoringBuddydata(BUDDY , accctrl.getContactList().get(i).getLoginId(),accctrl.getContactList().get(i).getNickname(),accctrl.getContactList().get(i).getAccount(),accctrl.getContactList().get(i).getMobile(),"",accctrl.getContactList().get(i).getLogo(),"","","",1,classuser.getLogin());
//                          if (accctrl.getContactList().get(i).getLoginId()==MySqliteUtlis.GetInstan(context).showdata(BUDDY,classuser.getLogin()).get(i).getM_account_id()){
//                              MySqliteUtlis.GetInstan(context).UpdateIsfriends(BUDDY,accctrl.getContactList().get(i).getLoginId());
//                          }
                            }
                            Buddy_ListData = MySqliteUtlis.GetInstan(context).showdata(BUDDY,classuser.getLogin());
                            if(Buddy_ListData.size()<0) {
                                return;}else{
                             testDuiViews.getDataAda(Buddy_ListData,classuser.getLogin(),1);
                            }
                        }
                    }
                    break;
            }


        }
    };

    /**
     * 获取本地好友列表
     */
    protected void initLocalData() {
        try {
            Buddy_ListData = MySqliteUtlis.GetInstan(context).showdata(BUDDY,classuser.getLogin());
            if (Buddy_ListData.size() < 0) {
                Buddy_ListData.clear();
               testDuiViews.setOffUpdata();
                return;
            } else {
                testDuiViews.getDataAda(Buddy_ListData,ClassUser.getLogin(),2);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    class MyBroadCat extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            /**
             * 0  代表文本
             * 1  开门
             * 2
             */
            Log.e("main","广播执行");
            switch (intent.getIntExtra("messageFlag",0)){
                case 0:
                    break;
                case 1:
                    break;
                case 2:
                    break;
                case 3:
                    break;
                case 4:
                    break;
                case 5:
                    break;
                case 6:
                    break;
                case 7:
                    break;
                case 8:
                    break;
                case 50:
                    initLocalData();
                    break;

            }
        }
    }

    public void onBackPressed(){
        if (mybroad!=null){
           context. unregisterReceiver(mybroad);}
    }

}
